package org.mystock.candlestick.service.pattern;

import org.joda.time.DateTime;
import org.mystock.candlestick.dao.DailyMetricsDao;
import org.mystock.candlestick.dao.EventTypeDao;
import org.mystock.candlestick.model.DailyData;
import org.mystock.candlestick.model.Event;
import org.mystock.candlestick.model.EventType;
import org.mystock.candlestick.model.Ticker;
import org.mystock.candlestick.service.util.CandleUtil;

public class BullOrBearEngulfing implements Matcher {

	EventTypeDao eventTypeDao;
	DailyMetricsDao dailyDataDao;

	Ticker ticker;

	public BullOrBearEngulfing(Ticker ticker) {
		this.ticker = ticker;
	}

	@Override
	public Event match() {

		Event event = new Event();
		DateTime toDate = new DateTime();

		DailyData today = dailyDataDao.getDataByDate(ticker, toDate);
		DailyData yesterday = dailyDataDao.getDataByDate(ticker, toDate
				.minusDays(1));

		if (CandleUtil.isEngulfing(yesterday, today)) {
			if (CandleUtil.getBodyColor(yesterday).equals("white")
					&& CandleUtil.getBodyColor(today).equals("black")) {
				event.setEventType(eventTypeDao
						.getEventByCode(EventType.BULLISH_ENGULFING));
				event.setComment("Bullish engulfing");
			} else if (CandleUtil.getBodyColor(yesterday).equals("white")
					&& CandleUtil.getBodyColor(today).equals("black")) {
				event.setEventType(eventTypeDao
						.getEventByCode(EventType.BEARISH_ENGULFING));
				event.setComment("Bearish Engulfing");
			} else {
				event = null;
			}
		}
		return event;
	}
}
